package org.game.netty.session;

import org.game.constant.SessionConstent;
import org.game.netty.oldcoder.other.EncryptionAndDecryptionUtil;
import org.game.netty.oldcoder.other.IChannelConstants;

import com.sojoys.artifact.constant.SessionStatusEnum;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.factory.session.ISessionFactory;

import io.netty.channel.Channel;
import io.netty.util.Attribute;

public class NettySessionFactory implements ISessionFactory<Channel>{
	
	public ISession createSession(Channel channel){
		ISession session = new NettySession(channel);
		session.setStatus(SessionStatusEnum.RUNNING);
		// 取得解密钥匙
		Attribute<int[]> decryptionKeysAttribute = channel.attr(IChannelConstants.DECRYPTION_KEYS_ATTRIBUTE_KEY);
		int[] decryptionKeys = null;
		if((decryptionKeys=decryptionKeysAttribute.get())==null){
			// 设置默认解密密钥
			decryptionKeys = EncryptionAndDecryptionUtil.getDefaultCustomDecryptionKeys();
			decryptionKeysAttribute.set(decryptionKeys);
		}
		// 取得加密钥匙
		int[] encryptionKeys = EncryptionAndDecryptionUtil.getDefaultCustomEncryptionKeys();
		
		session.setAttr(SessionConstent.ENCRYPTION_KEYS, encryptionKeys);
		session.setAttr(SessionConstent.DECRYPTION_KEYS, decryptionKeys);
		return session;
	}
}
